--[[
    渐变系统主要用于配置化的style缓动渐变效果。
    与tween不同的是，它只能做单次渐变。
]]
TransitionSystem = System.new()
TransitionSystem.transitions = {}

function TransitionSystem:update(dt)
    for k, v in pairs(self.transitions) do
        v.time = v.time + dt
        -- 删除已完成的transition
        if v.time >= v.duration then
            self.transitions[k] = nil
        end
    end
end

function TransitionSystem:add(element, oldState, newState)
    -- 找出最大duration
    local duration = element.styleConfig.easing.duration or 0
    for k, v in pairs(element.styleConfig.easing) do
        if v.duration then
            duration = math.max(duration, v.duration)
        end
    end
    self.transitions[element] = {
        time = 0,
        duration = duration,
        from = oldState,
        to = newState
    }
end

function TransitionSystem:rgba(element)
    -- if not self.transitions[element] then
        return element.style.color:unpack()
    -- end
    -- local easing = element.styleConfig.easing
    -- if easing and easing.color then
    --     local tra = self.transitions[element]
    --     local time = tra.time
    --     local duration = easing.color.duration or easing.duration
    --     local p = math.min(1, time / duration)
    --     p = easing.color.method(p)

    --     local r, g, b, a = element.styleConfig[tra.from].color:unpack()
    --     local r2, g2, b2, a2 = element.styleConfig[tra.to].color:unpack()
    --     return math.lerp(r, r2, p), math.lerp(g, g2, p), math.lerp(b, b2, p), math.lerp(a, a2, p)
    -- else
    --     local r, g, b, a = element.styleConfig[element.styleState].color:unpack()
    --     return r, g, b, a
    -- end
end

RegisterSystem(TransitionSystem)